/*******************************************************************************
 * Software Name : Android Simple Downloader
 *
 * Copyright © 2012 Rinvay Tang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.rinvay.asdownloader.ui;


import com.rinvay.asdownloader.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

/**
 * view web resource
 * @author Rinvay.T
 * @date 2012-4-7
 * @time 下午11:41:11
 */
public class WebViewActivity extends Activity
{
	EditText address;
	WebView web;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);
		web = (WebView) findViewById(R.id.wv_web);
		web.getSettings().setJavaScriptEnabled(true);
		web.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				address.setText(url);
				view.loadUrl(url);
				return true;
			}
		});
		web.setDownloadListener(new DownloadListener() {

			public void onDownloadStart(String url, String userAgent,
					String contentDisposition, String mimetype,
					long contentLength) {
				String filename = com.rinvay.utils.URLUtil.guessFileName(url, contentDisposition, mimetype);
				
				Intent downloadIntent = new Intent(WebViewActivity.this,
						ASDownloaderActivity.class);
				downloadIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				downloadIntent.setAction(ASDownloaderActivity.ACTION_NEW_TASK);
				Bundle bundle = new Bundle();
				bundle.putString("url", url);
				bundle.putString("filename", filename);
				downloadIntent.putExtras(bundle);
				startActivity(downloadIntent);
			}
		});
		web.loadUrl(formatUrl("http://wap.sogou.com/music/index.jsp"));

		address = (EditText) findViewById(R.id.et_url);
		//		address.clearFocus();

		Button go = (Button) findViewById(R.id.btn_go);
		go.setOnClickListener(onclickListener);
		Button back = (Button) findViewById(R.id.btn_back);
		back.setOnClickListener(onclickListener);

		web.requestFocus();
	}

	private OnClickListener onclickListener = new OnClickListener() {

		public void onClick(View v) {
			switch (v.getId())
			{
			case R.id.btn_go:
				String url = formatUrl(address.getText().toString());
				web.loadUrl(url);
				break;
			case R.id.btn_back:
				if(web.canGoBack()) {
					web.goBack();
				}
				break;
			}
		}
	};

	private String formatUrl(String url) {
		if (!URLUtil.isValidUrl(url))
		{
			url = "http://" + url;
		}
		return url;
	}

}
